
************* DISCRETE CHOICE EXPERIMENT 2 (education outcomes) ***************
						
	use "$root/output/analysis_finaldataset.dta", clear

// Reshape Data for Analysis	
/*
Right now, the data is in wide shape. To run the regression, we need the data
in long format, i.e we need to have only one column with a "choice" dummy.
The question, which asks them to make a choice, will also be in one column.
So the same respondent id will be repeated for 8 lines (4 questions that
asks them to make choices, two choice options in each)
*/

	// Keep relevant data
	keep google male lowexp agencyid minister region subnational country_id responseid male current_emp dce2_r1 dce2_r2 dce2_r3 dce2_r4 rd_1_a_gainfoundationalliteracy rd_1_b_gainfoundationalliteracy rd_2_a_gainfoundationalliteracy rd_2_b_gainfoundationalliteracy rd_3_a_gainfoundationalliteracy rd_3_b_gainfoundationalliteracy rd_4_a_gainfoundationalliteracy rd_4_b_gainfoundationalliteracy rd_1_a_dutifulcitizens rd_1_b_dutifulcitizens rd_2_a_dutifulcitizens rd_2_b_dutifulcitizens rd_3_a_dutifulcitizens rd_3_b_dutifulcitizens rd_4_a_dutifulcitizens rd_4_b_dutifulcitizens rd_1_a_completesecondaryschool rd_1_b_completesecondaryschool rd_2_a_completesecondaryschool rd_2_b_completesecondaryschool rd_3_a_completesecondaryschool rd_3_b_completesecondaryschool rd_4_a_completesecondaryschool rd_4_b_completesecondaryschool experience region
	gen 	agency2=agencyid==11
	replace agency2=. if agencyid==.
		
	// Rename
	**ROUND 1, BUNDLE CHOICES A AND B should end with 1 and 2 for each category
		ren (rd_1_a_gainfoundationalliteracy rd_1_b_gainfoundationalliteracy rd_1_a_completesecondaryschool rd_1_b_completesecondaryschool rd_1_a_dutifulcitizens rd_1_b_dutifulcitizens) ///
			   (rd1_gainfoundationalliteracy1 rd1_gainfoundationalliteracy2  rd1_completesecondaryschool1 rd1_completesecondaryschool2 rd1_dutifulcitizens1 rd1_dutifulcitizens2)

	**ROUND 2, BUNDLE CHOICES A AND B should end with 3 and 4 for each category
		ren (rd_2_a_gainfoundationalliteracy rd_2_b_gainfoundationalliteracy rd_2_a_completesecondaryschool rd_2_b_completesecondaryschool rd_2_a_dutifulcitizens rd_2_b_dutifulcitizens) ///
			(rd1_gainfoundationalliteracy3 rd1_gainfoundationalliteracy4  rd1_completesecondaryschool3 rd1_completesecondaryschool4 rd1_dutifulcitizens3 rd1_dutifulcitizens4)

	**ROUND 3, BUNDLE CHOICES A AND B should end with 5 and 6 for each category	
		ren (rd_3_a_gainfoundationalliteracy rd_3_b_gainfoundationalliteracy rd_3_a_completesecondaryschool rd_3_b_completesecondaryschool rd_3_a_dutifulcitizens rd_3_b_dutifulcitizens) ///
			(rd1_gainfoundationalliteracy5 rd1_gainfoundationalliteracy6  rd1_completesecondaryschool5 rd1_completesecondaryschool6 rd1_dutifulcitizens5 rd1_dutifulcitizens6)

	**ROUND 4, BUNDLE CHOICES A AND B should end with 7 and 8 for each category
		ren (rd_4_a_gainfoundationalliteracy rd_4_b_gainfoundationalliteracy rd_4_a_completesecondaryschool rd_4_b_completesecondaryschool rd_4_a_dutifulcitizens rd_4_b_dutifulcitizens) ///
			(rd1_gainfoundationalliteracy7 rd1_gainfoundationalliteracy8  rd1_completesecondaryschool7 rd1_completesecondaryschool8 rd1_dutifulcitizens7 rd1_dutifulcitizens8)

		ren (dce2_r1 dce2_r2 dce2_r3 dce2_r4) (dce2_r1 dce2_r3 dce2_r5 dce2_r7) // **here we know which bundle respondent chose
	
	// Drop respondents who didn't participate in experiment
	drop if rd1_completesecondaryschool1==.

	// Generate new ID and reshape to long 
	*Note: each respondent should appear 8 times in the dataset as each one participated in 4 rounds which consisted of two options
	gen id=_n
	reshape long rd1_gainfoundationalliteracy rd1_completesecondaryschool rd1_dutifulcitizens dce2_r, i(id) j(exp)
	
	// Clean rounds and choice answers
	**when we reshaped the data we kept the respondents' choice only in the odd bundles (BUNDLE "A" ORIGINALLY - 1, 3, 5 and 7). We also lost the round numbers, and the bundles are no longer 1 and 2, rather a continuos block of 8. Let's now re-create the rounds, and rename bundles to 1 and 2:
	
	** first replace empty cells with the respondent's choice:
	replace dce2_r=dce2_r[_n-1] if dce2_r==""

	** number the rounds:
	gen 	round=1 if exp<3
	replace round=2 if exp==3 | exp==4
	replace round=3 if exp==5 | exp==6
	replace round=4 if exp==7 | exp==8

	** rename bundles into 1 and 2 instead of 1-8
	gen 	experiment=1 if exp==1 | exp==3 | exp==5 | exp==7
	replace experiment=2 if exp==4 | exp==6 | exp==8| exp==2

	** generate a choice dummy:
	gen 	choice=1 if experiment==1 & dce2_r=="A"
	replace choice=1 if experiment==2 & dce2_r=="B"
	replace choice=0 if choice==. & dce2_r!=""
	
	//people that put none for all 4 rounds:	
	gen 	no_dce=.
	replace no_dce=1 if inlist(id, 43, 78, 95, 100, 137, 140, 182, 196)
	replace no_dce=1 if inlist(id,207, 221, 233, 252, 283, 311, 324, 410)
	replace no_dce=1 if inlist(id, 417, 23)
	replace no_dce=1 if inlist(id,449, 476, 525, 586, 668, 672, 675, 681)
	replace no_dce=1 if inlist(id,696, 700, 706, 719, 733, 766, 813, 851)
	
	save "$root/output/analysis_dce_outcomes.dta", replace
			
	drop if choice==.
	drop if rd1_completesecondaryschool==.

	replace choice=0 if dce2_r=="C"


// Document bundles with no trade-offs 	
	sort  id round choice
	order id round experiment choice
	
	*gen difference between items in the bundle chosen and the one left behind
	*read as: subtract the value before if it is the same round
	gen dum_dif_gain=rd1_gainfoundationalliteracy-rd1_gainfoundationalliteracy[_n-1] if round==round[_n-1]
	gen dum_dif_complete=rd1_completesecondaryschool-rd1_completesecondaryschool[_n-1] if round==round[_n-1]
	gen dum_dif_citizen=rd1_dutifulcitizens-rd1_dutifulcitizens[_n-1] if round==round[_n-1]
	
	*gen dummy to indicate no trade off between bundles
	gen 	no_trades=1 if dum_dif_gain==0 & dum_dif_complete==0 & dum_dif_citizen==0 // *no trade offs if there is no difference between all bundle items
	replace no_trades=1 if dum_dif_gain==0 & dum_dif_citizen==0 | dum_dif_gain==0 & dum_dif_complete==0 | dum_dif_citizen==0 & dum_dif_complete==0 	// no trade offs if there is no difference between two bundle items
	replace no_trades=1 if dum_dif_gain>0 & dum_dif_complete>0 & dum_dif_citizen==0 | dum_dif_citizen>0 & dum_dif_complete>0 & dum_dif_gain==0 | dum_dif_gain>0 & dum_dif_citizen>0 & dum_dif_complete==0 // 			*no trade offs if dif betwen two items across bundles is positive and the third is zero
	replace no_trades=1 if dum_dif_gain>0 & dum_dif_citizen>0 & dum_dif_complete>0 & dum_dif_citizen!=. // 			*no trade offs if all items are better in one of the bundles
	replace no_trades=0 if no_trades==. & dum_dif_citizen!=. // 	*the rest have tradeoffs		
	tab no_trades

	replace no_trades=no_trades[_n+1] if no_trades==.

	// Generate interaction variables 
	foreach var of varlist male experience agency2 {
	gen `var'_gain=`var'*rd1_gainfoundationalliteracy
	gen `var'_complete=`var'*rd1_completesecondaryschool
	gen `var'_citizens=`var'*rd1_dutifulcitizens
	}
								
	lab var rd1_gainfoundationalliteracy "\% with Foundational Literacy" 
	lab var rd1_completesecondaryschool  "\% Completing Secondary School"
	lab var rd1_dutifulcitizens			 "\% Dutiful Citizens"

	global controls3 male lowexp minister subnational i.country_id

	replace rd1_gainfoundationalliteracy=rd1_gainfoundationalliteracy/10 
	replace rd1_completesecondaryschool=rd1_completesecondaryschool/10 
	replace rd1_dutifulcitizens=rd1_dutifulcitizens/10

	lab var rd1_gainfoundationalliteracy "+10\% point with FL"
	lab var rd1_completesecondaryschool "+10\% point complete secondary"
	lab var rd1_dutifulcitizens "+10\% point are dutiful citizens"

	egen pair=group(id round)

********************************************************************************	
	
// Table 5 
preserve
drop if dce2_r=="C"
	eststo clear
	eststo m1: qui reghdfe choice rd1_gainfoundationalliteracy rd1_completesecondaryschool rd1_dutifulcitizens 				, nocons cluster(pair) a(id)
		test rd1_gainfoundationalliteracy=rd1_completesecondaryschool
		test rd1_dutifulcitizens=rd1_completesecondaryschool
		test rd1_dutifulcitizens=rd1_gainfoundationalliteracy
	eststo m2: qui reghdfe choice rd1_gainfoundationalliteracy rd1_completesecondaryschool rd1_dutifulcitizens if agency2==1, nocons cluster(pair) a(id)
		test rd1_gainfoundationalliteracy=rd1_completesecondaryschool
		test rd1_dutifulcitizens=rd1_completesecondaryschool
		test rd1_dutifulcitizens=rd1_gainfoundationalliteracy
	eststo m3: qui reghdfe choice rd1_gainfoundationalliteracy rd1_completesecondaryschool rd1_dutifulcitizens if agency2==0, nocons cluster(pair) a(id)
		test rd1_gainfoundationalliteracy=rd1_completesecondaryschool
		test rd1_dutifulcitizens=rd1_completesecondaryschool
		test rd1_dutifulcitizens=rd1_gainfoundationalliteracy
	estfe m1 m2 m3, labels(id "Respondent FE")
		
	esttab using tables/dce_outcomes.tex, replace booktabs f ///
		   label stats(N df_a r2,fmt(%9.0gc %9.0gc 3 )labels("Obs. (Responses)" "Obs. (Respondents)"  "R\sym{2}")) se ///
		   nonumbers ///
		   starlevels(* 0.1 ** 0.05 *** 0.01) noomitted ///
		   indicate(`r(indicate_fe)',  label("Yes" "")) ///
		   mtitle("Sample" "Agency" "\& agencies") 	

	estout, replace type style(smcl) ///
	   label stats(N df_a r2,fmt(%9.0gc %9.0gc 3)labels("N (Responses)" "N (Respondents)" "R2")) cells(b(star fmt(3)) se(par)) starlevels(* 0.1 ** 0.05 *** 0.01) noomitted ///
	   indicate(`r(indicate_fe)', label("Yes" "")) ///
	   collabel(none) mlabel("" "",numbers) nobaselevels		
restore

// Table B6: Conjoint Experiment: Education Outcomes Preferences (Logit Model)
	
preserve
drop if dce2_r=="C"
	eststo clear
	eststo m5:	qui logit choice rd1_gainfoundationalliteracy rd1_completesecondaryschool rd1_dutifulcitizens 	     , cluster(pair)
	eststo m1: 	   margins, dydx(rd1_gainfoundationalliteracy rd1_completesecondaryschool rd1_dutifulcitizens ) post
			
	eststo m6:	qui logit choice rd1_gainfoundationalliteracy rd1_completesecondaryschool rd1_dutifulcitizens i.id , cluster(pair)
	eststo m2:	   margins, dydx(rd1_gainfoundationalliteracy rd1_completesecondaryschool rd1_dutifulcitizens ) post			
				
	eststo m7:	qui logit choice rd1_gainfoundationalliteracy rd1_completesecondaryschool rd1_dutifulcitizens i.id    if agency2==1, cluster(pair)
	eststo m3: 	   margins, dydx(rd1_gainfoundationalliteracy rd1_completesecondaryschool rd1_dutifulcitizens ) post

	eststo m8:	qui logit choice rd1_gainfoundationalliteracy rd1_completesecondaryschool rd1_dutifulcitizens i.id    if agency2==0, cluster(pair)
	eststo m4: 	   margins, dydx(rd1_gainfoundationalliteracy rd1_completesecondaryschool rd1_dutifulcitizens ) post

	esttab m1 m2 m3 m4 using tables/dce_outcomes_logmargins.tex, replace booktabs f ///
		label stats(N,fmt(%9.0gc %9.0gc 3 )labels("Obs. (Responses)")) se ///
		starlevels(* 0.1 ** 0.05 *** 0.01) noomitted nonumbers ///
		mtitle("Sample" "Sample" "Agency" "\& agencies") b("%12.4f")

	esttab m5 m6 m7 m8 using tables/dce_outcomes_log.tex, replace booktabs f ///
		label stats(N N_clust r2_p,fmt(%9.0gc %9.0gc 3 )labels("Obs. (Responses)" "Obs. (Respondents)"  "Pseudo R\sym{2}")) se ///
		drop(_cons) nonumbers ///
		starlevels(* 0.1 ** 0.05 *** 0.01) noomitted ///
		mtitle("Sample" "Sample" "Agency" "\& agencies") ///
		indicate("Resp. FE = *id",  label("Yes" ""))
restore
	
// Table C3: Conjoint Experiment: Education Outcomes Preferences
preserve
	drop if dce2_r=="C"
	keep if googlelist==1
	replace choice=choice*100
	eststo clear
	eststo m1: qui reghdfe choice rd1_gainfoundationalliteracy rd1_completesecondaryschool rd1_dutifulcitizens		 		, cluster(pair) a(id)
	eststo m2: qui reghdfe choice rd1_gainfoundationalliteracy rd1_completesecondaryschool rd1_dutifulcitizens if agency2==1, cluster(pair) a(id)
	eststo m3: qui reghdfe choice rd1_gainfoundationalliteracy rd1_completesecondaryschool rd1_dutifulcitizens if agency2==0, cluster(pair) a(id)
	estfe m1 m2 m3, labels(id "Respondent FE")

	estout, replace type style(smcl) ///
	   label stats(N df_a r2,fmt(%9.0gc %9.0gc 3)labels("N (Responses)" "N (Respondents)" "R2")) cells(b(star fmt(3)) se(par)) starlevels(* 0.1 ** 0.05 *** 0.01) noomitted ///
	   keep(rd1_gainfoundationalliteracy rd1_completesecondaryschool rd1_dutifulcitizens) ///
	   collabel(none) mlabel("" "",numbers) nobaselevels	
	
	esttab using tables/dce_outcomes_robustness.tex, replace booktabs f ///
		   label stats(N df_a r2,fmt(%9.0gc %9.0gc 3 )labels("Obs. (Responses)" "Obs. (Respondents)"  "R\sym{2}")) se ///
		   keep(rd1_gainfoundationalliteracy rd1_completesecondaryschool rd1_dutifulcitizens) nonumbers ///
		   starlevels(* 0.1 ** 0.05 *** 0.01) noomitted ///
		   indicate(`r(indicate_fe)',  label("Yes" "")) ///
		   mtitle("Sample" "Agency" "\& agencies") 	
restore
